﻿using DevExpress.XtraGrid.Views.BandedGrid;
using DLL;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.InfantBaby
{
    public partial class HistoryInfantBaby : Form
    {
        public NursingFormSystem.NurFormsService.NursingForm formService = new NursingFormSystem.NurFormsService.NursingForm();
        Tb_User LoginUser;
        PatientInfo Patientinfo;
        public HistoryInfantBaby(Tb_User loginUser, PatientInfo patientInfo)
        {
            InitializeComponent();
            LoginUser = loginUser;
            Patientinfo = patientInfo;
            if ((Patientinfo.PATIENT_ID.Contains("$1$1")) || (Patientinfo.PATIENT_ID.Contains("$2$2")))//临时处理方式，需检查基本信息表
            {
                Patientinfo.PATIENT_ID = Patientinfo.PATIENT_ID.Substring(0, Patientinfo.PATIENT_ID.Length - 2);
            }
            BindControl();
            BindInfo();
        }

        #region 查询
        private void btnchaxun_Click(object sender, EventArgs e)
        {
            string StatDate = this.StatDate.Text;
            string EndDate = this.EndDate.Text;

            if (string.IsNullOrEmpty(StatDate) || string.IsNullOrEmpty(EndDate))
            {
                MessageBox.Show("查询日期不能为空！");
            }
            BindInfo();

        }
        #endregion

        #region 新增
        private void btnxinzeng_Click(object sender, EventArgs e)
        {
            AddInfantBaby addForm = new AddInfantBaby(LoginUser, Patientinfo, "");
            addForm.ShowDialog();
            if (addForm.DialogResult == DialogResult.OK)
            {
                BindInfo();
            }

        }
        #endregion

        #region 修改
        private void Btnxiugai_Click(object sender, EventArgs e)
        {
            if (bandedGridView1.RowCount > 0)
            {
                string Id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                AddInfantBaby addForm = new AddInfantBaby(LoginUser, Patientinfo, Id);
                addForm.ShowDialog();
                if (addForm.DialogResult == DialogResult.OK)
                {
                    BindInfo();
                }
            }

        }
        #endregion

        #region 打印
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            InfantBabyPrint f = new InfantBabyPrint(LoginUser, Patientinfo, StatDate.DateTime, EndDate.DateTime);
            //InfantBabyPrints f = new InfantBabyPrints(LoginUser, Patientinfo, StatDate.Text, EndDate.Text);
            f.Show();

        }
        #endregion

        #region 删除
        private void simpleButton2_Click(object sender, EventArgs e)
        {
            if (bandedGridView1.RowCount <= 0) return;
            if (MessageBox.Show("确定要删除数据吗？", "删除前确认", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                string id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                var ret2 = formService.DeleteNursingFormRecord(id);
                MessageBox.Show(ret2.M);
                if (ret2.Code == 1)
                {
                    string dateStart = StatDate.Text;
                    string dateEnd = EndDate.Text;
                    BindInfo();
                }
            }

        }
        #endregion

        #region GridView设置
        private void BindControl()
        {
            StatDate.DateTime = DateTime.Now.AddDays(-30).Date;
            EndDate.DateTime = DateTime.Now;
            foreach (GridBand gridBand in this.bandedGridView1.Bands)//设置居中
            {
                foreach (GridBand gridBandSecond in gridBand.Children)
                {
                    gridBandSecond.OptionsBand.FixedWidth = true;
                    gridBandSecond.AppearanceHeader.Options.UseTextOptions = true;
                    gridBandSecond.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                    gridBandSecond.AppearanceHeader.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
                }
                gridBand.OptionsBand.FixedWidth = true;
                gridBand.AppearanceHeader.Options.UseTextOptions = true;
                gridBand.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                gridBand.AppearanceHeader.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
            }
        }
        #endregion

        #region LoadData显示
        private void BindInfo()
        {
            if (Patientinfo == null) return;
            int VisitId = Patientinfo.VISIT_ID;//住院次数
            int FormId = (int)FormType.BybyAndBaby;  //表单ID
            DataTable dt = new DataTable();
            dt.Columns.Add("", typeof(string));
            dt.Columns.Add("riqi", typeof(string));
            dt.Columns.Add("shijian", typeof(string));
            dt.Columns.Add("tiwen", typeof(string));
            dt.Columns.Add("fanying", typeof(string));
            dt.Columns.Add("fuse", typeof(string));
            dt.Columns.Add("chunmuru", typeof(string));
            dt.Columns.Add("peifangnai", typeof(string));
            dt.Columns.Add("dabiancishu", typeof(string));
            dt.Columns.Add("dabianyanse", typeof(string));
            dt.Columns.Add("xiaobiancishu", typeof(string));
            dt.Columns.Add("xingzhi", typeof(string));
            dt.Columns.Add("liang", typeof(string));
            dt.Columns.Add("qidai", typeof(string));
            dt.Columns.Add("kusheng", typeof(string));
            dt.Columns.Add("yingerqingkuangjichuli", typeof(string));
            dt.Columns.Add("hushiqianming", typeof(string));
            dt.Columns.Add("Id", typeof(string));
            var ret = formService.GetNursingFormRecordByDate(Patientinfo.PATIENT_ID, Patientinfo.VISIT_ID, FormId, this.StatDate.DateTime, this.EndDate.DateTime);
            if (ret.Code == 1)
            {
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                list = list.OrderByDescending(p => p.AssessmentDate).ToList();
                foreach (NursingFormRecord record in list)
                {
                    var NursingFormData = record.NursingFormData;
                    Byays fd = new Byays();
                    fd = XmlUtil.XMLToObject<Byays>(NursingFormData, Encoding.UTF8);
                    DataRow row = dt.NewRow();
                    row["riqi"] = !string.IsNullOrEmpty(fd.RiQi) ? Convert.ToDateTime(fd.RiQi).ToString("yyyy-MM-dd") : string.Empty;
                    row["shijian"] = DateTimeHelper.HHmmssToHHmm(fd.ShiJian);
                    row["tiwen"] = fd.YiBanQingKuangTiWen;
                    row["fanying"] = fd.YiBanQingKuangFanYing;
                    row["fuse"] = fd.YiBanQingKuangFuSe;
                    row["chunmuru"] = fd.WeiYangChunMuRu;
                    row["peifangnai"] = fd.WeiYangPeiFangNai;
                    row["dabiancishu"] = fd.DaBianCiShu;
                    row["dabianyanse"] = fd.DaBianYanSe;
                    row["xiaobiancishu"] = fd.XiaoBianCiShu;
                    row["xingzhi"] = fd.OuTuXingZhi;
                    row["liang"] = fd.OuTuLiang;
                    row["qidai"] = fd.QiDai;
                    row["kusheng"] = fd.KuSheng;
                    row["yingerqingkuangjichuli"] = fd.YingErQingKuangJiChuLi;
                    row["hushiqianming"] = fd.HuShiQianMing;
                    //ID隐藏，双击触发事件
                    row["Id"] = record.Id;
                    dt.Rows.Add(row);
                }
            }
            dt.DefaultView.Sort = "riqi DESC,shijian DESC";
            DataTable data = dt.DefaultView.ToTable();
            gridControl1.DataSource = data;
        }
        #endregion

        private void bandedGridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (e.Clicks == 2)
            {
                if (bandedGridView1.RowCount > 0)
                {
                    string Id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                    AddInfantBaby addForm = new AddInfantBaby(LoginUser, Patientinfo, Id);
                    addForm.ShowDialog();
                    if (addForm.DialogResult == DialogResult.OK)
                    {
                        BindInfo();
                    }
                }
            }
        }
    }
}